﻿using Decoration.Comm;
using Decoration.IService;
using Decoration.Models;
using Decoration.Models.Dto;
using Decoration.Models.DTO;
using Decoration.Models.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.IdentityModel.Tokens;

namespace Decoration.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class WF_NodeController : ControllerBase
    {
        IWF_NodeService svc;
        public WF_NodeController(IWF_NodeService _svc)
        {
            this.svc = _svc;
        }
        [HttpGet]
        public PagedResult<WF_NodeDto> Query(string? NodeName, int PageSize = 10, int PageIndex = 1)
        {
            string sql = @" select  n.Id,TypeName,NodeCode,NodeName,r.RoleName,IsUseDept,PrevNodeCode,NextNodeCode,n.Status 
            from WF_Nodes n left join WF_ApprovalFlows t on n.TypeCode = t.TypeCode 
            left join Base_RoleInfos r on r.Id = n.RoleId where 1=1 ";
            if (!NodeName.IsNullOrEmpty())
            {
                sql += " and NodeName like @NodeName";
            }
            var TotalSql = " select count(*) num from(" + sql + ") a";

            var Total = svc.ExecuteScalar<int>(sql, new { DictName = "%" + NodeName + "%" });

            sql += @" order By id 
                        offset @PageSize*(@PageIndex-1) rows fetch next @PageSize rows only";

            var list = svc.Query<WF_NodeDto>(sql, new { NodeName = "%" + NodeName + "%", PageSize, PageIndex }).ToList();
            return new PagedResult<WF_NodeDto>() { code = 1, msg = "成功",total= Total, data = list };
        }
        [HttpGet("{id}")]
        public Result Get(int id)
        {
            var info = svc.Get(o => o.Id == id);
            if (info == null)
            {
                return new Result() { code = -1, msg = "未查询到数据" };
            }
            return new Result() { code = 1, msg = "成功", data = info };

        }
        [HttpPost]
        public Result Post(WF_Node node)
        {
            var info = new WF_Node()
            {
                Id = node.Id,
                RoleId = node.RoleId,
                RoleName = node.RoleName,
                IsUseDept = node.IsUseDept,
                TypeCode = node.TypeCode,
                NodeName = node.NodeName,
                NodeCode=node.NodeCode,
                NextNodeCode=node.NextNodeCode,
                PrevNodeCode=node.PrevNodeCode,
                CreateTime = DateTime.Now,
                Status = 1
            };
            if (info == null)
            {
                return new Result() { code = -1, msg = "数据不能为空" };
            }
            var res = svc.Add(info);
            return new Result() { code = 1, msg = "成功", data = res };
        }

        // PUT api/<DictinfoController>/5
        [HttpPut]
        public Result Put(WF_Node dict)
        {
            var getId = svc.Get(o => o.Id == dict.Id);
            if (getId == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            getId.PrevNodeCode = dict.PrevNodeCode;
            getId.NextNodeCode = dict.NextNodeCode;
            getId.RoleName = dict.RoleName;
            getId.RoleId = dict.RoleId;
            getId.NodeName = dict.NodeName;
            getId.NodeCode = dict.NodeCode;
            getId.TypeCode = dict.TypeCode;
            getId.IsUseDept=dict.IsUseDept;
            getId.Status = dict.Status;
            var res = svc.Update(getId);
            return new Result() { code = 1, msg = "成功", data = res };

        }

        // DELETE api/<DictinfoController>/5
        [HttpDelete("{id}")]
        public Result Delete(int id)
        {
            var getId = svc.Get(o => o.Id == id);
            if (getId == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            getId.Status = 0;
            var res = svc.Update(getId);
            return new Result() { code = 1, msg = "成功", data = getId };

        }
        /// <summary>
        /// 获取节点对应的记录
        /// </summary>
        /// <param name="Id">应用的Id</param>
        /// <param name="ApplicationTypeId">应用类型请假，加班，公告</param>
        /// <param name="TypeCode">流程编号</param>
        /// <returns></returns>
        ///
        [HttpGet]
        [Route("GetNodeRecord")]
        public PagedResult<WF_NodeDto> GetNodeRecord(int Id, int ApplicationTypeId, int TypeCode)
        {
            string SQL = @"select n.*,r.OperatorId,r.Operator,ApprovalOpinions,r.ApprovalStatus
                          from WF_Node n
                          left join WF_Record r on n.NodeCode=r.NodeCode and r.ApplicationId=@Id and r.ApplicationTypeId=@ApplicationTypeId
                          where n.typeCode=@TypeCode";

            var info = svc.Query<WF_NodeDto>(SQL,new {Id=Id,ApplicationTypeId = ApplicationTypeId,TypeCode=TypeCode });
            return new PagedResult<WF_NodeDto> { code = 1, msg = "成功", data = info };
        }
    }
}
